package com.gt.notify;

import com.gt.common.CommonConstant;
import com.gt.entity.project.Project;
import com.gt.entity.user.User;
import com.gt.service.project.ProjectService;
import com.gt.service.user.UserService;
import com.gt.utils.EmailUtil;
import com.gt.utils.EmailUtil2;
import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.List;

/**
 * 发送通知邮件
 */
public class SendEmail {
    private static Logger logger = Logger.getLogger(SendEmail.class);
    /**
     * 插件集成测试邮件通知
     * @param name
     * @param message
     * @param apiKey
     * @param userService
     * @param javaMailSender
     * @param sender
     * @param projectService
     * @throws Exception
     */
    public static void notifyUser(String name, String message, String apiKey, UserService userService, JavaMailSender javaMailSender, String sender, ProjectService projectService) throws Exception{
        User user = userService.querySelectUserByApikey(apiKey);
        if(null != user){
            Project query = new Project();
            query.setProjectApikey(apiKey);
            List<Project> projectList = projectService.queryList(query);
            if(null != projectList && !projectList.isEmpty()){
                String forgetUrl = CommonConstant.loginurl.getCode().toString();
                StringBuilder sb = new StringBuilder();
                sb.append("<html>");
                sb.append("<body>");
                sb.append("<center>");
                sb.append("<div style=\"margin-top:0px;background-color: #F09B22;width: 70%; height: 40%;padding: 10% 20%;\">");
                sb.append("<p><img src=\"http://39.106.249.147:9999/css/assets/img/logo_red.png\" style=\"width: 250px; height: 80px;\" /></p>");
                sb.append("<p style=\"color: white\">尊敬的");
                sb.append(user.getNickName()+"您的"+projectList.get(0).getProjectName()+"项目有错误信息产生,请及时查看。");
                sb.append("</p>");
                sb.append("<p style=\"background-color:#3498db;border-radius:10px;width: 150px; height: 50px; line-height: 50px; border: none;color:white;margin-top: 50px;\">");
                sb.append("<a href=\""+forgetUrl+"\" style=\"text-decoration:none;color:white;\">查看详情</a>");
                sb.append("</p>");
                sb.append("</div>");
                sb.append("</center>");
                sb.append("</body>");
                sb.append("</html>");
                new Thread(() -> {
                    try {
                        boolean flag = EmailUtil2.sendEmail(1, "gtdebug", user.getUserName(), projectList.get(0).getProjectName()+"项目错误报警", sb.toString());
                        if(!flag){
                            EmailUtil2.sendEmail(2, "gtdebug", user.getUserName(), projectList.get(0).getProjectName()+"项目错误报警", sb.toString());
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }).start();
            }else{
                logger.info("apiKey="+apiKey+",的项目不存在！");
            }
        }else{
            logger.error("apiKey="+apiKey+",的用户不存在");
        }
    }

    /**
     * 产生错误定时邮件通知
     * @param projectName
     * @param email
     * @param javaMailSender
     * @param sender
     * @throws Exception
     */
    public static void notifyUser2(String projectName, String email, JavaMailSender javaMailSender, String sender) throws Exception{
        String forgetUrl = CommonConstant.loginurl.getCode().toString();
        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        sb.append("<body>");
        sb.append("<center>");
        sb.append("<div style=\"margin-top:0px;background-color: #F09B22;width: 70%; height: 40%;padding: 10% 20%;\">");
        sb.append("<p><img src=\"http://39.106.249.147:9999/css/assets/img/logo_red.png\" style=\"width: 250px; height: 80px;\" /></p>");
        sb.append("<p style=\"color: white\">尊敬的gtdebug用户您好，");
        sb.append("您的"+projectName+"项目有错误信息产生,请及时查看。");
        sb.append("</p>");
        sb.append("<p style=\"background-color:#3498db;border-radius:10px;width: 150px; height: 50px; line-height: 50px; border: none;color:white;margin-top: 50px;\">");
        sb.append("<a href=\""+forgetUrl+"\" style=\"text-decoration:none;color:white;\">查看详情</a>");
        sb.append("</p>");
        sb.append("</div>");
        sb.append("</center>");
        sb.append("</body>");
        sb.append("</html>");
        new Thread(() -> {
            try {
                boolean flag = EmailUtil2.sendEmail(1, "gtdebug", email, projectName+"项目错误报警", sb.toString());
                if(!flag){
                    EmailUtil2.sendEmail(2,"gtdebug", email, projectName+"项目错误报警", sb.toString());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

}
